//
//  SelectGoodsNameCell.m
//  NtoccShipper
//
//  Created by mac on 2019/12/3.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "SelectGoodsNameCell.h"
#import "SelectGoodsNameCellModel.h"

@interface SelectGoodsNameCell(){
    UIView* _imgParent;
//    int index;  // 记录图片的位置
}
@property (nonatomic,strong) NSMutableArray *arrModel;
@end

@implementation SelectGoodsNameCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)removeCell:(UIGestureRecognizer*)sender
{
    
    UIView* cell = sender.view;
    UILabel *label = (UILabel *)[cell viewWithTag:1000 + cell.tag - 100];
    SelectGoodsNameCellModel *model = self.arrModel[cell.tag - 100];

    if (model.selected) {
        model.selected = NO;
        cell.backgroundColor = [UIColor cjWhiteColor];
        label.textColor = [UIColor cjMainTextColor];

    }else{
        model.selected = YES;
        cell.backgroundColor = [UIColor cjMainColor];
        label.textColor = [UIColor cjWhiteColor];
    }
}

- (void)setCellInfoWithMutaArray:(NSMutableArray *)arrMuta withHeaderModel:(SelectGoodsNameHeaderModel *)headerModel;
{
    [_imgParent removeAllSubviews];

    [arrMuta reverse];
    
//    index = 0;
    for (int i =0; i < arrMuta.count; i++) {
        
//            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeCell:)];
//            UIView* cell = [[UIView alloc]init];
//
//            cell.tag = index;
//        index++;
//            [cell enableFlexLayout:YES];
//            [cell addGestureRecognizer:tap];
//
//            [cell setLayoutAttrStrings:@[
//                                         @"width",@"65",
//                                         @"height",@"65",
//                                         @"margin",@"1.5%",
//                                         @"alignItems",@"center",
//                                         @"justifyContent",@"center",
//                                         ]];
//            [cell setViewAttr:@"bgColor" Value:@"#e5e5e5"];
//            [cell setViewAttr:@"borderRadius" Value:@"10"];
//            [_imgParent insertSubview:cell atIndex:0];
//    //        _imgParent.backgroundColor = [UIColor cjRedColor];
//
//            [_imgParent markDirty];
//            [self layoutIfNeeded];
        SelectGoodsNameCellModel *model = arrMuta[i];

//        if (headerModel.isCreate) {
////            UILabel *label = (UILabel *)[self viewWithTag:i+100];
////            [_imgParent removeFromSuperview];
//        }else{
            headerModel.isCreate = YES;
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeCell:)];
        
//            UIView *viewOld = (UIView *)[self viewWithTag:i + 100];
//            if (viewOld) {
//                [viewOld removeAllSubviews];
//                [viewOld removeFromSuperview];
//            }

            UIView* cell = [[UIView alloc]init];
            cell.tag = i + 100;
            [cell enableFlexLayout:YES];
            [cell addGestureRecognizer:tap];

            [cell setLayoutAttrStrings:@[
                                       @"minHeight",@"30",
                                       @"margin",@"1.5%",
                                       @"alignItems",@"center",
                                       @"justifyContent",@"center",

                                       ]];
            [cell setViewAttr:@"borderRadius" Value:@"5"];
            [cell setViewAttr:@"borderWidth" Value:@"0.5"];
            [cell setViewAttr:@"borderColor" Value:@"#E4E9ED"];


            UILabel *lab = [[UILabel alloc]init];
            lab.tag = 1000 + i;
            [lab enableFlexLayout:YES];
            [lab setLayoutAttrStrings:@[

                                 @"flex",@"1",
                                 ]];

            [cell addSubview:lab];
            //          [lab setViewAttr:@"textAlign" Value:@"center"];
            [lab setViewAttr:@"fontSize" Value:@"14"];
            [lab setViewAttr:@"color" Value:@"#434C67"];

            lab.text = CJStringWithFormat(@"  %@  ",model.name);
            NSAttributedString * attR = [[NSAttributedString alloc] initWithString:lab.text attributes:@{NSFontAttributeName: lab.font,NSBaselineOffsetAttributeName:@(2)}];
            [lab setAttributedText:attR];

            [cell markDirty];
            [_imgParent insertSubview:cell atIndex:0];

            [_imgParent markDirty];

            [self layoutIfNeeded];
            

        if (model.selected) {
             cell.backgroundColor = [UIColor cjMainColor];
             lab.textColor = [UIColor cjWhiteColor];
         }else{
             cell.backgroundColor = [UIColor cjWhiteColor];
             lab.textColor = [UIColor cjMainTextColor];
         }
                
    }
    
    self.arrModel = [[NSMutableArray alloc]init];
    self.arrModel = arrMuta;
    
}


@end
